package com.example.chapter06;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;

import com.example.chapter06.database.UserDBHelper;
import com.example.chapter06.enity.User;
import com.example.chapter06.util.ToastUtil;

import java.util.List;

public class SQLiteHelperActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
    private EditText et_name;
    private EditText et_age;
    private EditText et_height;
    private EditText et_weight;
    private int marriage;
    private RadioGroup rg_marriage;
    private UserDBHelper mHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite_helper);

        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        et_height = findViewById(R.id.et_height);
        et_weight = findViewById(R.id.et_weight);
        rg_marriage = findViewById(R.id.rg_marriage);
        rg_marriage.setOnCheckedChangeListener(this);

        findViewById(R.id.logon).setOnClickListener(this);
        findViewById(R.id.delete).setOnClickListener(this);
        findViewById(R.id.update).setOnClickListener(this);
        findViewById(R.id.query).setOnClickListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        System.out.println(radioGroup);
        System.out.println(i);
        marriage = i;
    }

    @Override
    protected void onStart() {
        super.onStart();
        //获取数据库帮助器的实例
        mHelper = UserDBHelper.getInstance(this);
        //打开数据库帮助器的读写链接
        mHelper.openWriteLink();
        mHelper.openReadLink();
    }

    @Override
    protected void onStop() {
        super.onStop();
        //关闭数据库链接
        mHelper.closeLink();
    }

    @Override
    public void onClick(View view) {
        String name = et_name.getText().toString();
        String age = et_age.getText().toString();
        String height = et_height.getText().toString();
        String weight = et_weight.getText().toString();
        User user = null;
        if (view.getId() == R.id.logon) {
            user = new User(name,
                    Integer.parseInt(age),
                    Float.parseFloat(height),
                    Float.parseFloat(weight),
                    marriage);
            if (mHelper.insert(user) > 0) {
                ToastUtil.show(this, "添加成功");
            }
        } else if (view.getId() == R.id.delete) {
            if (mHelper.deleteByName(name) > 0) {
                ToastUtil.show(this, "删除成功");
            }
        } else if (view.getId() == R.id.update) {
            user = new User(name, Integer.parseInt(age), Float.parseFloat(height), Float.parseFloat(weight), marriage);
            if (mHelper.update(user) > 0) {
                ToastUtil.show(this, "更新成功");
            }
        } else if (view.getId() == R.id.query) {
//            List<User> list= mHelper.queryAll();
            List<User> list= mHelper.queryByName(name);
            for (User u : list) {
                Log.d("ning",u.toString());
            }
        }
    }
}